package top.xbaoziplus.company.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.xbaoziplus.company.common.CommonPage;
import top.xbaoziplus.company.common.CustomException;
import top.xbaoziplus.company.common.Result;
import top.xbaoziplus.company.empty.Staff;
import top.xbaoziplus.company.empty.User;
import top.xbaoziplus.company.empty.UserAccount;
import top.xbaoziplus.company.empty.UserInfo;
import top.xbaoziplus.company.mapper.UserAccountMapper;
import top.xbaoziplus.company.mapper.UserInfoMapper;
import top.xbaoziplus.company.mapper.UserMapper;
import top.xbaoziplus.company.service.UserService;

import javax.annotation.Resource;

/**
 *
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Resource
    private UserAccountMapper userAccountMapper;

    @Override
    public boolean isExist(Long jobId) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getJobId, jobId);
        return ObjectUtils.isNotEmpty(userMapper.selectOne(queryWrapper));
    }

    @Override
    public UserInfo findByJobIdAndPassword(Long jobId, String password) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getJobId, jobId);
        queryWrapper.eq(UserInfo::getPassword, password);
        // 获取用户信息对象
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        // 对用户信息对象进行判断
        if (ObjectUtils.isEmpty(userInfo)) {
            throw new CustomException("密码错误，请检查密码是否输入正确");
        }
        if (userInfo.getStatus().equals(Staff.OUT_WORK)) {
            throw new CustomException("员工已离职，无法使用本系统");
        }
        if (userInfo.getAcStatus().equals(User.DESTROYED)) {
            throw new CustomException("该账号已被拉入黑名单，请联系管理员");
        }
        return userInfo;
    }

    @Override
    public Result getAllPage(CommonPage commonPage, Integer type) {
        LambdaQueryWrapper<UserAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserAccount::getStatus, type);
        queryWrapper.eq(UserAccount::getAuthority, 0);
        if (!ObjectUtils.isEmpty(commonPage.getKey())){
            queryWrapper.like(UserAccount::getName, commonPage.getKey());
        }
        Page<UserAccount> page = userAccountMapper.selectPage(new Page<UserAccount>(commonPage.getCurrentPage(), commonPage.getPageSize()), queryWrapper);
        return Result.success(page, "成功");
    }
}




